package com.ptszyxx.popose.module.main.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.MineFollowTabEnum;
import com.ptszyxx.popose.databinding.FragmentMineFollowTabBinding;
import com.ptszyxx.popose.module.main.mine.adapter.MineFollowAdapter;
import com.ptszyxx.popose.module.main.mine.adapter.MineIntimacyAdapter;
import com.ptszyxx.popose.module.main.mine.vm.MineFollowTabVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.mine.FollowEntity;
import com.ysg.http.data.entity.mine.IntimacyEntity;
import com.ysg.utils.YCollectionUtil;

import java.util.List;

public class MineFollowTabFragment extends BaseFragment<FragmentMineFollowTabBinding, MineFollowTabVM> {

    private MineFollowTabEnum tabEnum;
    private MineFollowAdapter followAdapter;//关注和粉丝共用
    private MineIntimacyAdapter intimacyAdapter;

    public MineFollowTabFragment(MineFollowTabEnum tabEnum){
        this.tabEnum = tabEnum;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine_follow_tab;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MineFollowTabVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MineFollowTabVM.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        viewModel.tabEnum = tabEnum;
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        followAdapter = new MineFollowAdapter(viewModel);
        intimacyAdapter = new MineIntimacyAdapter(viewModel);

        switch (tabEnum){
            case intimacy:
                binding.refreshView.initAdapter(intimacyAdapter);
                break;
            case follow:
            case fans:
                binding.refreshView.initAdapter(followAdapter);
                break;
        }
    }

    @Override
    public void initData() {
        super.initData();
        switch (tabEnum){
            case intimacy:
                viewModel.requestIntimacyList();
                break;
            case follow:
                viewModel.requestFollowList(true);
                break;
            case fans:
                viewModel.requestFansList(true);
                break;
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, new Observer<List<FollowEntity>>() {
            @Override
            public void onChanged(List<FollowEntity> list) {
                setAdapterUI(list);
            }
        });
        viewModel.uc.onRefreshIntimacyEvent.observe(this, new Observer<List<IntimacyEntity>>() {
            @Override
            public void onChanged(List<IntimacyEntity> list) {
                setAdapterIntimacyUI(list);
            }
        });
        viewModel.uc.onRefreshItemEvent.observe(this, o -> setAdapterItemUI());
    }


    private void setAdapterUI(List<FollowEntity> list) {
        if (YCollectionUtil.isNotEmpty(list) && MineFollowTabEnum.follow ==viewModel.tabEnum){
            for (FollowEntity item: list) {
                item.setGuanzhu(true);
            }
        }
        binding.refreshView.setAdapterUI(followAdapter, list, viewModel.isRefresh());
    }

    private void setAdapterIntimacyUI(List<IntimacyEntity> list) {
        binding.refreshView.setAdapterUI(intimacyAdapter, list, viewModel.isRefresh());
    }

    private void setAdapterItemUI() {
        switch (tabEnum){
            case follow:
                followAdapter.remove(viewModel.followEntity);
                break;
            case fans:
                viewModel.followEntity.setGuanzhu(!viewModel.followEntity.isGuanzhu());
                followAdapter.notifyItemChanged(viewModel.position);
                break;
        }
    }

}
